﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QLTV.DataTier;
using QLTV.ControllerTier;

namespace QLTV.Pages
{
    /// <summary>
    /// Interaction logic for GetSach.xaml
    /// </summary>
    public partial class GetSach : UserControl
    {
        CQLTV ql = new CQLTV();
        bool MuonOK = true;
        public GetSach()
        {
            InitializeComponent();
            dgSach1.Visibility = System.Windows.Visibility.Hidden;
            tb.Text = string.Empty;
            CSACH cSach = new CSACH();
            //cSach.LoadData(ref dgSach);

            //SACH mysach = new SACH() 
            //{ 
            //    GiaSach = 65000,
            //    TacGia = "Nguyễn Thành Tâm",
            //    TenSach = "Tâm",
            //    NXB = "Tâm",
            //    NamXB = 2014,
            //    NgayNhapSach = System.DateTime.Now,
            //    TinhTrang = "Con",
            //    MaTheLoai = 1,
            //};

            //int i = 60;
            
            //ql.ThemSach(mysach);
            //cSach.Kiemtra(mysach);
            //cSach.LoadData(dgSach);
            dgSach.ItemsSource = cSach.Tim("Trinh Thám");

        }

        private void tb_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (tb.Text.ToString() != string.Empty)
            {
                int i = 0;
                try
                {
                    i = int.Parse(tb.Text.ToString());
                    if (i <= 0)
                    {
                        MuonOK = false;
                        tb1.Text = "nhập sai mã độc giả";
                        tb1.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        MuonOK = true;
                        tb1.Visibility = System.Windows.Visibility.Hidden;
                    }
                } catch (Exception ex)
                {
                    //MessageBox.Show("loi");
                    //error dock
                    MuonOK = false;
                    tb1.Text = "nhập sai mã độc giả";
                    tb1.Visibility = System.Windows.Visibility.Visible;
                }
                if (ql.ThongTinDocGia(i) != null)
                {
                    dgSach1.ItemsSource = ql.ThongTinDocGia(i);
                    dgSach1.Visibility = System.Windows.Visibility.Visible;
                }
            }
            else
            {
                tb1.Text = "nhập mã độc giả";
                tb1.Visibility = System.Windows.Visibility.Visible;
                MuonOK = false;
                dgSach1.ItemsSource = null;
                //MessageBox.Show("nhap ma doc gia");
                
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (MuonOK)
            {
                //cho muon
                tb2.Text = "OK";
                tb2.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                tb2.Text = "KO";
                tb2.Visibility = System.Windows.Visibility.Visible;
            }
            tb.Text = string.Empty;
            dgSach1.Visibility = System.Windows.Visibility.Hidden;
            tb1.Visibility = System.Windows.Visibility.Hidden;
        }
    }
}
